﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Antioch.Base
{
    [Jemgine.Engine.Node, Jemgine.Engine.Category("Save State Variables")]
    public class SaveState_SetVariable : Jemgine.Data.ScriptNode
    {
        public String Variable { get; set; }
        public int Value { get; set; }

        public SaveState_SetVariable()
        {
            AddInput("Fire", null);
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Jemgine.Data.ScriptPayload Payload)
        {
            var GSM = Sim.GetModule<GameStateModule>();
            if (GSM.ActiveSaveState.Variables.ContainsKey(Variable)) GSM.ActiveSaveState.Variables[Variable] = Value;
            else GSM.ActiveSaveState.Variables.Add(Variable, Value);

            OutputTerminals[0].Fire(Payload);
        }
    }

    [Jemgine.Engine.Node, Jemgine.Engine.Category("Save State Variables")]
    public class SaveState_ToggleBoolean : Jemgine.Data.ScriptNode
    {
        public String Variable { get; set; }

        public SaveState_ToggleBoolean()
        {
            AddInput("Fire", null);
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Jemgine.Data.ScriptPayload Payload)
        {
            var GSM = Sim.GetModule<GameStateModule>();
            if (GSM.ActiveSaveState.Variables.ContainsKey(Variable))
            {
                if (GSM.ActiveSaveState.Variables[Variable] == 0)
                    GSM.ActiveSaveState.Variables[Variable] = 1;
                else
                    GSM.ActiveSaveState.Variables[Variable] = 0;

            }
            else GSM.ActiveSaveState.Variables.Add(Variable, 1);

            OutputTerminals[0].Fire(Payload);
        }
    }

    [Jemgine.Engine.Node, Jemgine.Engine.Category("Save State Variables")]
    public class SaveState_If : Jemgine.Data.ScriptNode
    {
        public enum _Condition
        {
            Equal,
            Greater,
            LessThan,
            GreaterEqual,
            LessThanEqual,
            NotEqual
        }

        public _Condition Condition { get; set; }
        public String Variable { get; set; }
        public int Value { get; set; }

        public SaveState_If()
        {
            AddInput("Fire", null);
            AddOutput("Then", null);
            AddOutput("Else", null);
        }

        public override void FireTerminal(int InputID, Jemgine.Data.ScriptPayload Payload)
        {
            var GSM = Sim.GetModule<GameStateModule>();
            bool Pass = false;
            if (!GSM.ActiveSaveState.Variables.ContainsKey(Variable))
                GSM.ActiveSaveState.Variables.Add(Variable, 0);
            switch (Condition)
            {
                case _Condition.Equal:
                    Pass = GSM.ActiveSaveState.Variables[Variable] == Value;
                    break;
                case _Condition.Greater:
                    Pass = GSM.ActiveSaveState.Variables[Variable] > Value;
                    break;
                case _Condition.GreaterEqual:
                    Pass = GSM.ActiveSaveState.Variables[Variable] >= Value;
                    break;
                case _Condition.LessThan:
                    Pass = GSM.ActiveSaveState.Variables[Variable] < Value;
                    break;
                case _Condition.LessThanEqual:
                    Pass = GSM.ActiveSaveState.Variables[Variable] <= Value;
                    break;
                case _Condition.NotEqual:
                    Pass = GSM.ActiveSaveState.Variables[Variable] != Value;
                    break;
            }

            if (Pass) OutputTerminals[0].Fire(Payload);
            else OutputTerminals[1].Fire(Payload);
        }
    }
}
